using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PietschSoft.VE;

namespace SecurityLinkUp.Admin
{
    public partial class ChapterAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // before we do anything, ensure that the user has the right
            // role to perform anything on this page
            Security.CheckAdminUser(this);

            if (Roles.IsUserInRole("SuperAdmin"))
            {
                AdminModeChk.Visible = true;
                DeleteBtn.Visible = true;
            }

            if (!Page.IsPostBack)
            {
                // add the status items
                foreach (string s in System.Enum.GetNames(typeof(CommonData.Status)))
                {
                    StatusList.Items.Add(s);
                }

                // populate the chapters list
                ArrayList chapters = ChapterData.GetUserAdminChapters((Guid)Membership.GetUser().ProviderUserKey);
                ChapterSelect.Items.Add("");
                foreach (Chapter c in chapters)
                {
                    ListItem itm = new ListItem(c.name + "  [" + c.status + "]", c.uid.ToString());
                    ChapterSelect.Items.Add(itm);
                }

                // populate the communities dropdown
                ArrayList communities = CommunityData.GetCommunities(CommonData.Status.ANY);
                foreach (Community c in communities)
                {
                    ListItem itm = new ListItem(c.name + "  [" + c.status + "]", c.uid.ToString());
                    ParentCommunity.Items.Add(itm);
                }
                
                // start off with the community and status lists disabled until we have a selected chapter
                StatusList.Enabled = false;
                ParentCommunity.Enabled = false;
            }
        }

        protected void AdminModeChk_CheckedChanged(object sender, EventArgs e)
        {
            // clear the old items
            ChapterSelect.Items.Clear();

            // repopulate the chapters list
            ArrayList chapters;
            if (!AdminModeChk.Checked)
            {
                // only show chapters where the user is an admin
                chapters = ChapterData.GetUserAdminChapters((Guid)Membership.GetUser().ProviderUserKey);
            }
            else
            {
                // super admin mode - show all communities
                chapters = ChapterData.GetChapters(CommonData.Status.ANY);
            }
            ChapterSelect.Items.Add("");
            foreach (Chapter c in chapters)
            {
                ListItem itm = new ListItem(c.name + "  [" + c.status + "]", c.uid.ToString());
                ChapterSelect.Items.Add(itm);
            }

            // clear the old data
            ClearData();
        }

        protected void ChapterSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            // clear the old data
            ClearData();

            // if the blank "start position" chapter is selected, exit now
            if (ChapterSelect.SelectedValue == "")
            {
                return;
            }

            // enable the status and parent community dropdowns
            StatusList.Enabled = true;
            ParentCommunity.Enabled = true;

            // get the chapter and set the basic properties
            Chapter c = ChapterData.GetChapter(new Guid(ChapterSelect.SelectedValue));
            ChapterName.Text = c.name;
            ChapterDescritpion.Text = c.description;
            ChapterReason.Text = c.statusnotes;

            // set the parent chapter
            ListItem communityitm = ParentCommunity.Items.FindByValue(c.parentcommunityid.ToString());
            if (communityitm != null)
            {
                int i = ParentCommunity.Items.IndexOf(communityitm);
                ParentCommunity.SelectedIndex = i;
            }
            else
            {
                ParentCommunity.SelectedIndex = 0;
            }

            // set the chapter status
            ListItem chapteritm = StatusList.Items.FindByValue(c.status.ToString());
            if (chapteritm != null)
            {
                int i = StatusList.Items.IndexOf(chapteritm);
                StatusList.SelectedIndex = i;
            }
            else
            {
                StatusList.SelectedIndex = 0;
            }
            
            // set the map data if appliciable
            Map.Pushpins.Clear();
            if (c.HasPosition)
            {
                GeoLat.Value = c.geolat.ToString();
                GeoLong.Value = c.geolong.ToString();
                ChapterGeoArea.Text = c.geoarea;
                ChapterLocationStatusImg.ImageUrl = "/Images/check.png";
                LatLong Position = new LatLong(c.geolat, c.geolong);
                Pushpin p = new PietschSoft.VE.Pushpin(c.uid.ToString(), Position, null, c.name, c.geoarea);
                Map.Pushpins.Add(p);
                Map.Latitude = c.geolat;
                Map.Longitude = c.geolong;
                Map.Zoom = 10;
            }
            else
            {
                GeoLat.Value = null;
                GeoLong.Value = null;
                ChapterLocationStatusImg.ImageUrl = "/Images/cross.png";
            }

        }

        // clear all the data from the form
        protected void ClearData()
        {
            // set the chapter data
            ChapterName.Text = "";
            ChapterDescritpion.Text = "";
            ChapterReason.Text = "";
            ParentCommunity.SelectedIndex = 0;
            StatusList.SelectedIndex = 0;

            // disable the status and parent community dropdowns
            StatusList.Enabled = false;
            ParentCommunity.Enabled = false;

            // set the map view
            GeoLat.Value = "";
            GeoLong.Value = "";
            ChapterGeoArea.Text = "";
            ChapterLocationStatusImg.ImageUrl = "/Images/cross.png";
            Map.Latitude = 0;
            Map.Longitude = 0;
            Map.Zoom = 1;

            ChapterErrorMessage.Text = "";
        }

        protected void SaveChapterBtn_Click(object sender, EventArgs e)
        {
            // if the blank "start position" chapter is selected, exit now
            if (ChapterSelect.SelectedValue == "")
            {
                return;
            }

            // create a chapter object
            Chapter c = new Chapter(new Guid(ChapterSelect.SelectedValue),
                                    new Guid(ParentCommunity.SelectedValue),
                                    ChapterName.Text,
                                    ChapterDescritpion.Text,
                                    (CommonData.Status)Enum.Parse(typeof(CommonData.Status),StatusList.SelectedValue, true),
                                    ChapterReason.Text);

            // if there's a geo location, add the details to the chapter
            if ((GeoLat.Value != "") && (GeoLong.Value != ""))
            {
                c.SetGeoData(ChapterGeoArea.Text, double.Parse(GeoLat.Value), double.Parse(GeoLong.Value));
            }

            // save the chapter to the database
            string result = ChapterData.SaveChapterToDB(c);
            if (result == "")
            {
                ChapterErrorMessage.Text = "Successfully saved.";
            }
            else
            {
                ChapterErrorMessage.Text = result;
            }
        }

        protected void ConfirmDeleteBtn_Click(object sender, EventArgs e)
        {

        }
    }
}
